#include "InputName.h"
#include "CGame.h"
#include "Rank.h"
#include "Title.h"



void InputName::InitScene()
{
	MzOpenSip();
	m_sprBackGround.CreateScript( SPR_TYPE_DEFAULT_CB, 240, 245 );
	m_sprBackGround.AddObject( CREATE_IMAGE_PKG_KEY( 0, 3, 0 ) );

	RECT rc = m_sprBackGround.GetScriptRect();
	rc.bottom -= 94;
	g_CInputData.AddInputObject( INPUT_NAME_BTN_ACTIVE, rc );

	rc = m_sprBackGround.GetScriptRect();
	rc.top += 140;
	rc.left += 290;
	rc.right -= 70;
	rc.bottom -= 30;
	g_CInputData.AddInputObject( INPUT_NAME_BTN_OK, rc );

}



InputName::InputName( CScene** ppScene )
{
	m_ppScene = ppScene;
	m_pPerScene = *ppScene;

	m_mapSprObj.clear();

	wcscpy( m_wszName, L"" );
	m_nNameSize = 0;

	StartLogic();

}



void InputName::RenderScene()
{
	m_pPerScene->RenderScene();
	m_sprBackGround.RenderScript();

	RECT rc = { 160, 120, 0, 0 };
	g_CDDraw.SetFontSize( 30, 800 );
	g_CDDraw.DDrawText( m_wszName, &rc );
	g_CDDraw.SetFontSize();

}



void InputName::RunLogic()
{
	if( m_bLogicStart )
	{
		WCHAR wszBuffer = g_CGame.GetInputBuffer();

		if( wszBuffer >= 32 && wszBuffer <= 126 )
		{
			if( ++m_nNameSize > DEFAULT_MAX_NAME_SIZE )
				m_nNameSize = DEFAULT_MAX_NAME_SIZE;
			else
			{
				int nStrSize = wcslen( m_wszName );
				m_wszName[nStrSize] = wszBuffer;
				m_wszName[nStrSize + 1] = L'\0';

			}

		}
		else if( wszBuffer > 256 )
		{
			m_nNameSize += 2;
			if( m_nNameSize > DEFAULT_MAX_NAME_SIZE )
				m_nNameSize -= 2;
			else
			{
				int nStrSize = wcslen( m_wszName );
				m_wszName[nStrSize] = wszBuffer;
				m_wszName[nStrSize + 1] = L'\0';

			}

		}
		else if( wszBuffer == 8 )
		{
			int nStrSize = wcslen( m_wszName );
			if( nStrSize > 0 )
			{
				if( m_wszName[nStrSize-1] > 256 )
					m_nNameSize -= 2;
				else
					--m_nNameSize;

				m_wszName[nStrSize - 1] = L'\0';

			}

		}

		if( g_CInputData.m_mapInputObject[INPUT_NAME_BTN_ACTIVE].m_nState == BTN_STATE_DOWN )
			MzOpenSip();
		else if( g_CInputData.m_mapInputObject[INPUT_NAME_BTN_OK].m_nState == BTN_STATE_DOWN )
		{
			if( m_nNameSize <= 0 )
				return ;

			MzCloseSip();
			g_CResource.PlaySound( SOUND_PUSH );

			m_pNextScene = new Rank( m_ppScene );
			m_pNextScene->m_pPerScene = new Title( m_ppScene );
			m_pNextScene->m_pPerScene->InitScene();
			m_pNextScene->m_pPerScene->StopLogic();
			m_pNextScene->InitScene();
			m_pNextScene->SetAnimate( ANIME_FADE_OUT, ANIME_CMD_START_LOGIC );

			if( m_dwRecordType == RECORD_TYPE_SCORE )
			{
				g_CGame.SaveHeightScore( m_dwParam, m_wszName );
				((Rank*)m_pNextScene)->SetScoreRank();

			}
			else if( m_dwRecordType == RECORD_TYPE_TIME )
			{
				g_CGame.SaveBossTime( m_dwParam, m_wszName );
				((Rank*)m_pNextScene)->SetBossTimeRank();

			}

			SetAnimate( ANIME_FADE_IN, ANIME_CMD_NEXT_SCENE | ANIME_CMD_DEL_THIS | ANIME_CMD_DEL_PER_SCENE );

		}

	}

}